<?php 
class Captcha_model extends Model {
   
   var $table_name       = 'ci_captcha';
   var $primary_key      = 'id';
   var $session_key      = 'session_id';
   var $value_key        = 'code';
   var $timestamp_key    = 'tst';
   var $timestamp_delay  = 600;
   
   function insert($session_id, $value) {
      $this->db->set($this->session_key, $session_id);
      $this->db->set($this->value_key, $value);
      $return = $this->db->insert($this->table_name);
      return $return;
   }
   
   function check($session_id, $value) {
      $return = false;      
      $this->db->where($this->session_key, $session_id);
      $this->db->where($this->value_key, $value);
      $query_result = $this->db->get($this->table_name);
      if ($query_result->num_rows() > 0) {
         $return = true;
      }
      $this->delete($session_id);
      return $return;
   }
   
   function delete($session_id) {
      $this->db->where($this->session_key, $session_id);
      $this->db->or_where($this->timestamp_key.' <', time()+(int)$this->timestamp_delay);
      return $this->db->delete($this->table_name);
   }   
}